import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2022-08-20
 * Time: 10:36
 */
public class ArrayWorkDemo {
    //1.求数组的平均值
    public static double avg(int[] array){
        if (array.length == 0 || array == null) return -1;
        int sum = 0;
        for (int i = 0; i < array.length; i++) {
           sum += array[i];
        }
        double avg = sum / array.length;
        return avg;
    }
    public static void main1(String[] args) {
        int[] array = { 1,2,3,4,5 };
        System.out.println(avg(array));
    }

    //2.数组所有元素之和
    public static int sum(int[] array){
        if (array == null) return -1;
        int sum = 0;
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        return sum;
    }
    public static void main2(String[] args) {
        int[] array = { 1,2,3,4,5 };
        System.out.println(sum(array));
    }

    //3.改变原有数组元素的值
    public static void transform(int[] array){
        for (int i = 0; i < array.length; i++) {
            array[i] = 2*array[i];
        }
    }
    public static void main3(String[] args) {
        int[] array = { 1,2,3,4,5 };
        transform(array);
        System.out.println(Arrays.toString(array));
    }

    //4.打印数组
    public static void printArray(int[] array){
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
    }
    public static void main4(String[] args) {
        int[] array = { 1,2,3,4,5 };
        printArray(array);
    }

    //5.创建的数组，并且赋初始值
    public static void main5(String[] args) {
        int[] array = new int[100];
        for (int i = 0; i < array.length; i++) {
            array[i] = i;
        }
        System.out.print(Arrays.toString(array) + " ");
    }

    //6.冒泡排序
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]){
                    int tmp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = tmp;
                }
            }
        }
    }
    public static void main6(String[] args) {
        int[] array = { 9,8,7,6,5,4,3,2,1 };
        bubbleSort(array);
        System.out.println(Arrays.toString(array));
    }

    //7.数组是否有序
    public static int judgeArray(int[] array){
        int i = 0;
        for (i = 0; i < array.length - 1; i++) {
            if (array[i] > array[i + 1]){
                break;
            }
        }
        if (i == array.length - 1){
            return 1;
        } else{
            return 0;
        }
    }
    public static void main7(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] array = new int[10];
        for (int i = 0; i < array.length; i++) {
            array[i] = scanner.nextInt();
        }
        int ret = judgeArray(array);
        if (ret == 1){
            System.out.println("有序");
        } else {
            System.out.println("无序");
        }
    }

    //8.二分查找
    public static  int binarySearch(int[] array, int n){
        int left = 0;
        int right = array.length - 1;
        int mid = 0;
        while (left < right){
            mid = left + (right - left) / 2;
            if (n < array[mid]){
                right = mid - 1;
            } else if (n > array[mid]){
                left = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }
    public static void main8(String[] args) {
        int[] array = { 1,2,3,4,5,6 };
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int ret = binarySearch(array, n);
        System.out.println(ret);
    }

    //9.数组的拷贝
    public static void copyOf(int[] array2, int[] array1){
        for (int i = 0; i < array1.length; i++) {
            array2[i] = array1[i];
        }
    }
    public static void main9(String[] args) {
        int[] array1 = { 1,2,3,4,5,6 };
        int[] array2 = new int[array1.length];
        copyOf(array2, array1);
        System.out.println(Arrays.toString(array2));
    }

    //10.数组转字符串
    public static String toString(int[] array){
        String str = "[";
        for (int i = 0; i < array.length; i++) {
            str += array[i];
            if (i < array.length - 1){
                str += ",";
            }
        }
        str += "]";
        return str;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] array = new int[10];
        for (int i = 0; i < array.length; i++) {
            array[i] = scanner.nextInt();
        }
        System.out.println(toString(array));
    }
}
